package nxu.it.security;

import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;

public class NeedLoginHandler implements Handler {
    public static final String LOGIN_SESSION_FIELD = "username";
    public static final String REDIRECT_SESSION_FILED = "redirect";
    @Override
    public void handle(Context ctx) throws Throwable{
        //没有登录,跳到登录页面
        if(ctx.session(LOGIN_SESSION_FIELD) == null){
            String requestPath = ctx.path();
            String queryString = ctx.queryString();
            StringBuffer redirectUrl = new StringBuffer();
            redirectUrl.append(requestPath);
            if(queryString != null && !queryString.isEmpty()){
                redirectUrl.append("?").append(queryString);
            }

            ctx.sessionSet(REDIRECT_SESSION_FILED,redirectUrl.toString());
            ctx.redirect("/login",307);
            ctx.setHandled(true);
        }
    }
}
